﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyTools.Helpers
{
    public static class DateTimeExtHelper
    {
        public static DateTime ToDateTime(this string str)
        {
            string[] formats = { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd", "yyyyMMdd", "MMdd" };
            if (DateTime.TryParse(str, out DateTime result))
            {
                return result;
            }
            str = str.PadLeft(4, '0');
            return DateTime.ParseExact(str, formats, null);
        }
    }
}
